package android_fields;

import static android_board.GUIBoard.FIELDWIDTH;
import static android_board.GUIBoard.FONTSIZE;
import common.GUIColor;
import common.IGUIField;
import android.graphics.Point;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android_board.GUIBoard;
import android_board.GUICenter;
import android_codebehind.AndroidComponentFactory;
import android_codebehind.CellOnClickListener;
import android_codebehind.GUIPlayer;
import dk.dtu.compute.course02312.matadordel2.MainActivity;

public abstract class AndroidField implements IGUIField {
	private static int nextNumber = 0;
	private int number;
	private int x;
	private int y;
	
	protected RelativeLayout cell;
	protected TextView titleView;
	protected TextView subTextView;
	private GUIColor bgColor;
	protected GUIColor fgColor;
	protected String title;
	protected String subText;
	protected String description;
	private boolean[] hasCars = new boolean[GUIBoard.MAX_PLAYER_COUNT];
	private ImageView[] cars;
	
	private static Point[] points = new Point[40];
	private static int nextPoint = 0;
	static {
		int i = 0;
		for (int x = 10; x > 0; x--) {
			AndroidField.points[i++] = new Point(x, 10);
		}
		for (int y = 10; y > 0; y--) {
			AndroidField.points[i++] = new Point(0, y);
		}
		for (int x = 0; x < 10; x++) {
			AndroidField.points[i++] = new Point(x, 0);
		}
		for (int y = 0; y < 10; y++) {
			AndroidField.points[i++] = new Point(10, y);
		}
	}

	public static class Builder<E> {
		protected String title = "Title";
		protected String subText = "subText";
		protected String description = "description";
		protected String rent = "Rent";
		protected GUIColor bgColor = GUIColor.LIGHT_GRAY;
		protected GUIColor fgColor = GUIColor.BLACK;

		@SuppressWarnings("unchecked")
		public E setFgColor(GUIColor fgColor) {
			this.fgColor = fgColor;
			return (E) this;
		}

		@SuppressWarnings("unchecked")
		public E setBgColor(GUIColor bgColor) {
			this.bgColor = bgColor;
			return (E) this;
		}
	}

	protected AndroidField(GUIColor bgColor, GUIColor fgColor, String title, String subText,
			String description) {
		nextNumber = (nextNumber % 40) + 1;
		this.number = nextNumber;
		Point p = getNextPlacement();
		this.cell = GUIBoard.getCell(p.x, p.y);
		this.cell.setBackgroundColor(bgColor.getRGB());
		this.cell.setOnClickListener(new CellOnClickListener(this));
		
		AndroidComponentFactory.createLineBorder(this.cell, 2,
				GUIColor.BLACK.getRGB());
		this.x = p.x;
		this.y = p.y;
		this.bgColor = bgColor;
		this.fgColor = fgColor;
		this.makeLabels();
		this.setTitle(title);
		this.setSubText(subText);
		this.setDescription(description);
		// this.layered.setBackgroundColor(bgColor.getRGB());
		// this.layered.setForeground(fgColor);
		// this.layered.setOpaque(true);
		// this.factory.setSize(this.layered, 1 * FIELDWIDTH, 1 * FIELDHEIGHT);
		// this.layered.addMouseListener(new FieldMouseListener(this));
		// this.layered.setLayout(new GridBagLayout());
	}

	private static Point getNextPlacement() {
		Point p = AndroidField.points[nextPoint];
		nextPoint = ++nextPoint % 40;
		return p;
	}

	private void makeLabels() {
		this.titleView = new TextView(MainActivity.context);
		this.titleView.setId(GUIBoard.generateViewId());
		this.titleView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
		this.titleView.setText(this.title);
		this.titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, FONTSIZE);
		this.subTextView = new TextView(MainActivity.context);
		this.subTextView.setId(GUIBoard.generateViewId());
		this.subTextView.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
		this.subTextView.setText(this.subText);
		this.subTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, FONTSIZE);
		RelativeLayout.LayoutParams subParams = new RelativeLayout.LayoutParams(
				FIELDWIDTH, LayoutParams.WRAP_CONTENT);
		subParams.addRule(RelativeLayout.ALIGN_BOTTOM, this.cell.findViewWithTag("cellsize").getId());
		this.cell.addView(this.subTextView, subParams);
		this.setFGColor(this.fgColor);
	}

	public boolean hasCar(String name) {
		GUIPlayer p = GUIBoard.getInstance().getPlayer(name);
		return this.hasCars[p.getNumber()];
	}

	public void setCar(String name, boolean hasCar) {
		GUIBoard board = GUIBoard.getInstance();
		GUIPlayer p = board.getPlayer(name);
		if (p != null) {
			this.hasCars[p.getNumber()] = hasCar;
			this.cars[p.getNumber()].setImageBitmap(p.getImage());
			this.cars[p.getNumber()].setVisibility(hasCar ? View.VISIBLE : View.GONE);
		}
	}

	public int getX() {
		return this.x;
	}

	public int getY() {
		return this.y;
	}

	public RelativeLayout getPanel() {
		return this.cell;
	}

	public void setTitle(String title) {
		this.title = title;
		this.titleView.setText(title);
	}

	public void setSubText(String subText) {
		this.subText = subText;
		this.subTextView.setText(subText);
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setCarIcons(ImageView[] cars) {
		this.cars = cars;
	}
	/**
	 * Sets the foregroundcolor of the field
	 * 
	 * @param fgColor
	 *            The foreground color
	 */
	private void setFGColor(GUIColor fgColor) {
		this.titleView.setTextColor(fgColor.getRGB());
		this.subTextView.setTextColor(fgColor.getRGB());
	}

	/**
	 * Each type of field displays information on the center
	 */
	public void displayOnCenter() {
		GUICenter.getInstance().clearCenter();
		GUICenter.getInstance().setBGColor(this.bgColor);
		GUICenter.getInstance().setFGColor(this.fgColor);
		GUICenter.textViews[0].setText("" + this.number);
		GUICenter.textViews[0].setVisibility(View.VISIBLE);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, 2);
		int margin = 2; 
		lp.setMargins(margin, 0, margin, 0);
		GUICenter.divider.setLayoutParams(lp);
		GUICenter.divider.setBackgroundColor(this.fgColor.getRGB());
		
		GUICenter.divider.setVisibility(View.VISIBLE);
		AndroidComponentFactory.createLineBorder(GUICenter.getInstance().getCenterPanel(), 4,
				this.fgColor.getRGB());
	}

	protected void displayCarOnCenter() {
		for (int i = 0; i < GUIBoard.MAX_PLAYER_COUNT; i++) {
			GUIPlayer p = GUIBoard.playerList[i];
			if (p != null && hasCar(p.getName())) {
				GUICenter.cars[i].setImageBitmap(p.getImage());
				GUICenter.cars[i].setVisibility(View.VISIBLE);
			} else {
				GUICenter.cars[i].setVisibility(View.GONE);
			}
		}
	}
}